package chap06;

import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

 class KeyEvent1 extends JFrame implements KeyListener {
    public static void main(String[] args) {
        new KeyEvent1();
    }
    public KeyEvent1(){
         init();
     }
    public void init(){
        JButton button = new JButton("OK");
        JTextField text = new JTextField();
        //设置JTextField 大小
        text.setPreferredSize(new Dimension(200,25));
        button.addKeyListener(this);
        text.addKeyListener(this);
        this.setLayout(new FlowLayout());
        this.add(button);
        this.add(text);
        this.setSize(300, 200);
        this.setVisible(true);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    }
    public void keyTyped(KeyEvent e){
        System.out.println("keyTyped");
    }


    public void keyPressed(KeyEvent e){
    Object obj=e.getSource();
        if (obj instanceof JTextField) {
            System.out.println("你按下的是" + e.getKeyChar());
        }
        if (obj instanceof JButton) {
            System.out.println("你按下的是"+e.getKeyChar());
        }
    }


    public void keyReleased(KeyEvent e){
        System.out.println("keyReleased");
    }

}
